/*
 * EnterURLDialog.java
 *
 * Created on 10. Juni 2007, 00:02
 */

package wordspaces.gui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import wordspaces.FileFetcher;

/**
 *
 * @author  alexander
 */
public class EnterURLDialog extends javax.swing.JPanel {
    
    private GUI gui;
    /**
     * Creates new form EnterURLDialog
     */
    public EnterURLDialog(GUI gui) {
        initComponents();
        this.gui = gui;
        dialog = new JDialog(gui,"Enter the URL's you like to import.",true);
        dialog.add(this);
        dialog.setSize(450,200);
        dialog.setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        urlTextArea = new javax.swing.JTextArea();
        fetchButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        urlTextArea.setColumns(20);
        urlTextArea.setRows(5);
        jScrollPane1.setViewportView(urlTextArea);

        fetchButton.setText("Fetch!");
        fetchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fetchButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(86, 86, 86)
                .add(fetchButton)
                .add(94, 94, 94)
                .add(cancelButton)
                .addContainerGap(100, Short.MAX_VALUE))
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(fetchButton)
                    .add(cancelButton)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dialog.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void fetchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fetchButtonActionPerformed
        final String[] urls = urlTextArea.getText().split("\\s");
        gui.progressBar.setMaximum(urls.length);
        final FileFetcher task = new FileFetcher(urls);
        task.addPropertyChangeListener(new PropertyChangeListener() {                      
            public  void propertyChange(PropertyChangeEvent evt) {
                if ("fileInfo".equals(evt.getPropertyName())) {
                    gui.textSizeLabel.setText("Downloading "+evt.getNewValue());
                }
                else if ("progress".equals(evt.getPropertyName())) {
                    gui.textSourcesListModel.addElement(evt.getNewValue());
                    gui.progressBar.setValue(gui.progressBar.getValue()+1);
                }
                else if(task.isDone()){
                    gui.textSizeLabel.setText("All "+urls.length+" files downloaded...");
                    gui.progressBar.setValue(0);
                }
             }
         });
        
        
        task.execute();
        /*
        for(String addr:urls){
            if(!addr.equals("")){
                try {
                    URL url = new URL(addr);
//                    
                } catch (MalformedURLException ex) {
                    JOptionPane.showMessageDialog(null, addr+" is no valid URL", "Warning", JOptionPane.ERROR_MESSAGE); 
                }  
            }
        }
         **/
        dialog.setVisible(false);
    }//GEN-LAST:event_fetchButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    protected javax.swing.JButton fetchButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea urlTextArea;
    // End of variables declaration//GEN-END:variables
    private JDialog dialog;
}
